//
// Created by WangPeng on 2024/8/15.
//

#ifndef RTSP_H264_SERVER_RTSP_PROTOCOL_H
#define RTSP_H264_SERVER_RTSP_PROTOCOL_H

#include <string>
#include <vector>
#include <atomic>

/*
 *RTSP协议，基于的是TCP协议，负责视频流的信令部分，不负责传输数据
 * RTP协议基于UDP协议，负责传输视频数据
 */
class CUdpSocket;
class CRtspCommand
{
public:
    explicit CRtspCommand(const std::string& req);
    CRtspCommand();
    ~CRtspCommand() = default;
    std::string response(std::string req_data, const std::string &client_ip);
    void loop_play(const std::string& av_stream_file = "/persistent/home/licks/h264/test.h264");
private:
    //command
    const std::string OPTION_CMD{"OPTIONS"};
    const std::string DESC_CMD{"DESCRIBE"};
    const std::string SETUP_CMD{"SETUP"};
    const std::string PLAY_CMD{"PLAY"};
    //request data
    std::string req_data_{};
    std::atomic_bool play_stream_{false};
    //rtp传输数据使用的udp
    std::shared_ptr<CUdpSocket> srv_rtp_fd_{nullptr};
    std::shared_ptr<CUdpSocket> srv_rtcp_fd_{nullptr};
private:
    static std::string handle_option(int sceq);
    static std::string handle_desc(int cseq, const char* url);
    static std::string handle_setup(int cseq, int client_rtp_port);
    static std::string handle_play(int cseq);
    static std::vector<std::string> split_string(const std::string& src, const std::string& delimiter);
    static int extract_number(const std::string& input);
    static std::pair<int, int> extract_ports(const std::string& input);
};

#endif //RTSP_H264_SERVER_RTSP_PROTOCOL_H
